

package top.continew.admin.question.user.service.questionset.entity;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import top.continew.admin.question.model.question.Question;
import top.continew.admin.question.model.question.QuestionKnowledgePoint;
import top.continew.admin.question.model.question.QuestionSet;
import top.continew.admin.question.user.service.question.enums.QuestionTypeEnum;
import top.continew.admin.question.user.service.questionset.enums.QuestionSetTypeEnum;

import java.util.List;

/**
 * 知识点题库
 */
public class KnowledgePointQuestionSet extends AbstractQuestionSet {

    private Long knowledgePointId;

    public KnowledgePointQuestionSet(Long objectId) {
        QuestionSet questionSet = new QuestionSet().selectOne(Wrappers.lambdaQuery(QuestionSet.class)
            .or(item -> item.eq(QuestionSet::getObjectId, objectId).or(item2 -> item2.eq(QuestionSet::getId, objectId)))
            .eq(QuestionSet::getType, QuestionSetTypeEnum.KNOWLEDGE_POINT_EXERCISE.getCode()));
        if (questionSet == null) {
            this.knowledgePointId = objectId;
            this.createQuestionSet(new QuestionSet());
        } else {
            this.knowledgePointId = questionSet.getObjectId();
            this.questionSet = questionSet;
        }
    }

    @Override
    public List<Long> getQuestionIdList() {
        //查询题集对应的知识点
        if (questionSet == null) {
            return List.of();
        }
        Long knowledgePointId = questionSet.getObjectId();
        //查询知识点对应的题
        List<QuestionKnowledgePoint> questionKnowledgePoints = new QuestionKnowledgePoint().selectList(Wrappers
            .lambdaQuery(QuestionKnowledgePoint.class)
            .eq(QuestionKnowledgePoint::getKnowledgeId, knowledgePointId));
        if (questionKnowledgePoints != null) {
            return questionKnowledgePoints.stream().map(QuestionKnowledgePoint::getQuestionId).toList();
        }
        return List.of();
    }

    @Override
    public long getQuestionCount() {
        if (questionSet == null) {
            return 0;
        }
        Long knowledgePointId = questionSet.getObjectId();
        MPJLambdaWrapper<QuestionKnowledgePoint> wrapper = new MPJLambdaWrapper<QuestionKnowledgePoint>()
            .select("distinct t.question_id")
            .leftJoin(Question.class, Question::getId, QuestionKnowledgePoint::getQuestionId)
            .eq(Question::getOnlineStatus, 1L)
            .ne(Question::getType, QuestionTypeEnum.MATERIAL_QUESTIONS.getCode())
            .eq(QuestionKnowledgePoint::getKnowledgeId, knowledgePointId);
        return new QuestionKnowledgePoint().selectCount(wrapper);
    }

    @Override
    public void createQuestionSet(QuestionSet questionSet) {
        //获取随机一个题目
        QuestionKnowledgePoint questionKnowledgePoint = new QuestionKnowledgePoint().selectOne(Wrappers
            .lambdaQuery(QuestionKnowledgePoint.class)
            .eq(QuestionKnowledgePoint::getKnowledgeId, knowledgePointId)
            .last("limit 1"));
        if (questionKnowledgePoint != null) {
            //存在题目进行创建题集
            questionSet.setTitle(questionKnowledgePoint.getName());
            questionSet.setObjectId(knowledgePointId);
            questionSet.setType(QuestionSetTypeEnum.KNOWLEDGE_POINT_EXERCISE.getCode());
            super.createQuestionSet(questionSet);
        }
    }
}
